package com.nbgroup.controller;


import com.nbgroup.pojo.Dept;
import com.nbgroup.pojo.common.Children;
import com.nbgroup.pojo.common.Result;
import com.nbgroup.pojo.dto.DeptDto;
import com.nbgroup.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-29
 */
@RestController
@RequestMapping("/system/dept")
public class DeptController {

    @Autowired
    private IDeptService deptService;

    /**
     * 部门列表查询
     */
    @GetMapping("/list")
    public Result listQueryDept(){
        List<Dept> depts = deptService.listQueryDept();
        return Result.success(depts);
    }

    /**
     * 新增部门
     */
    @PostMapping
    public Result add(@RequestBody DeptDto dto){
        deptService.add(dto);
        return Result.success();
    }

    /**
     * 删除部门
     */
    @DeleteMapping("/{id}")
    public Result delById(@PathVariable Integer id){
        deptService.removeById(id);
        return Result.success();
    }

    /**
     * 查询回显
     */
    @GetMapping("/{id}")
    public Result queryById(@PathVariable Integer id){
        Dept one = deptService.lambdaQuery().eq(Dept::getDeptId, id).one();
        return Result.success(one);
    }

    /**
     * 排除当前部门查询
     */
    @GetMapping("/list/exclude/{id}")
    public Result queryNotInId(@PathVariable Integer id){
        List<Dept> list = deptService.lambdaQuery().ne(Dept::getDeptId, id).list();
        return Result.success(list);
    }

    /**
     * 修改部门
     */
    @PutMapping
    public Result update(@RequestBody Dept dept){
        deptService.lambdaUpdate().eq(Dept::getDeptId,dept.getDeptId()).update(dept);
        return Result.success();
    }

    /**
     * 部门树查询
     */
    @GetMapping("/treeselect")
    public Result queryTree(){
        List<Children> list = deptService.queryTree(0);
        return Result.success(list);
    }
}
